﻿using Abp.Domain.Entities;
using Abp.Domain.Repositories;
using Mt.Site.Core.ContentManagement.Entities;
using Mt.Site.Core.StaticPages;
using Mt.Site.Core.Synchronization.Data;
using System;

namespace Mt.Site.Core.Synchronization.ContentManagement
{
    internal static class ContentSyncHelper
    {
        public static AutoSyncInfoStore CheckDataStatusForAutoSync(
            IRepository<Content, Guid> contentRepository,
            ISynchronizationHelper synchronizationHelper,
            int targetTenantId,
            string targetLanguage,
            Guid fromCotnentId,
            NoticeArgs notify)
        {
            var fromEntity = contentRepository.FirstOrDefault(fromCotnentId);
            if (fromEntity == null)
            {
                return null;
            }
            notify.FromId = fromEntity.Id;
            notify.MenuId = fromEntity.MenuId;
            notify.Title = fromEntity.Title;
            notify.ToLanguage = targetLanguage;
            notify.ToTenantId = targetTenantId;

            var toEntity = contentRepository.FirstOrDefault(
                    item => item.SourceID == fromCotnentId && item.Language == targetLanguage && item.TenantId == targetTenantId);

            var toNewId = Guid.Empty;
            var syncInfo = new AutoSyncInfoStore();
            if (toEntity != null)
            {
                syncInfo.IsAutoSync = synchronizationHelper.ShouldSynchronize(toEntity);
                if (syncInfo.IsAutoSync)
                {
                    notify.ToId = toEntity.Id;
                    syncInfo.ToId = toEntity.Id;
                    syncInfo.ToMenuId = toEntity.MenuId;
                    syncInfo.ToAuxiliaryId = toEntity.AuxiliaryId;
                    syncInfo.FromTenantId = fromEntity.TenantId;
                    syncInfo.FromLanguage = fromEntity.Language;
                    if (fromEntity is ISoftDelete)
                    {
                        syncInfo.IsFromEntityHasDeleted = (fromEntity as ISoftDelete).IsDeleted;
                    }
                    return syncInfo;
                }
            }
            return null;
        }

        public static AutoSyncInfoStore CheckDataStatusAndSyncForAutoSync(
            IRepository<Content, Guid> contentRepository,
            ISynchronizationHelper synchronizationHelper,
            IContentDataSoreManager contentDataSoreManager,
            IStaticPageHelper staticPageHelper,
            int targetTenantId,
            string targetLanguage,
            Guid fromCotnentId,
            NoticeArgs notify,
            bool isOnlySyncNotDeleted = false)
        {
            var fromEntity = contentRepository.FirstOrDefault(fromCotnentId);
            if (fromEntity == null)
            {
                return null;
            }
            var syncInfo = new AutoSyncInfoStore();

            if (fromEntity is ISoftDelete)
            {
                syncInfo.IsFromEntityHasDeleted = (fromEntity as ISoftDelete).IsDeleted;
            }
            if (isOnlySyncNotDeleted && syncInfo.IsFromEntityHasDeleted.HasValue && syncInfo.IsFromEntityHasDeleted.Value)
            {
                return null;
            }
 
            notify.FromId = fromEntity.Id;
            notify.MenuId = fromEntity.MenuId;
            notify.Title = fromEntity.Title;
            notify.ToLanguage = targetLanguage;
            notify.ToTenantId = targetTenantId;

            var toEntity = contentRepository.FirstOrDefault(
                    item => item.SourceID == fromCotnentId && item.Language == targetLanguage && item.TenantId == targetTenantId);

            var toNewId = Guid.Empty;

            if (toEntity == null)
            {
                toNewId = contentDataSoreManager.CreateContentSync(fromEntity,
                        new DataProcessingStore
                        {
                            TargetLanguage = targetLanguage,
                            ToTenantId = targetTenantId
                        });
                syncInfo.IsAutoSync = true;
            }
            else
            {
                syncInfo.IsAutoSync = synchronizationHelper.ShouldSynchronize(toEntity);
                if (syncInfo.IsAutoSync)
                {
                    toNewId = contentDataSoreManager.UpdateContentSync(toEntity, fromEntity);
                }
            }

            if (toNewId != Guid.Empty)
            {
                var entity = contentRepository.FirstOrDefault(toNewId);
                if (entity != null)
                {
                    staticPageHelper.DeleteListAndDetailPage(entity.TenantId, entity.Language, entity.MenuId, entity.AuxiliaryId);
                    staticPageHelper.DeleteStaticPage(entity.TenantId, entity.Language, 0);
                }
                syncInfo.ToId = toNewId;
                notify.ToId = toNewId;
                syncInfo.ToMenuId = entity.MenuId;
                syncInfo.ToAuxiliaryId = entity.AuxiliaryId;
                syncInfo.FromTenantId = fromEntity.TenantId;
                syncInfo.FromLanguage = fromEntity.Language;
                return syncInfo;
            }

            return null;
        }
   }
}
